<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="description" content="Kitchen Sink | Cypress Example">
  <meta name="author" content="Cypress.io">
  <meta name="copyright" content="Cypress.io, Inc">

  <title>Cypress.io: Kitchen Sink</title>

  <link rel="icon" href="/assets/img/favicon.ico" type="image/x-icon">
  <link rel="stylesheet" href="/assets/css/vendor/bootstrap.min.css">
  <link rel="stylesheet" href="/assets/css/vendor/fira.css">
  <link rel="stylesheet" href="/assets/css/styles.css">
</head>

<body>
  <nav class="navbar navbar-inverse">
    <div class="container">
      <div class="navbar-header">
        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar">
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
        <a class="navbar-brand" href="/">cypress.io</a>
      </div>
      <div id="navbar" class="navbar-collapse collapse">
        <ul class="nav navbar-nav">
          <li class="active" class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button">Commands <span class="caret"></span></a>
            <ul class="dropdown-menu">
              <li><a href="/commands/querying">Querying</a></li>
              <li><a href="/commands/traversal">Traversal</a></li>
              <li><a href="/commands/actions">Actions</a></li>
              <li><a href="/commands/window">Window</a></li>
              <li><a href="/commands/viewport">Viewport</a></li>
              <li><a href="/commands/location">Location</a></li>
              <li><a href="/commands/navigation">Navigation</a></li>
              <li><a href="/commands/assertions">Assertions</a></li>
              <li><a href="/commands/misc">Misc</a></li>
              <li><a href="/commands/connectors">Connectors</a></li>
              <li><a href="/commands/aliasing">Aliasing</a></li>
              <li><a href="/commands/waiting">Waiting</a></li>
              <li><a href="/commands/network-requests">Network Requests</a></li>
              <li><a href="/commands/files">Files</a></li>
              <li class="active"><a href="/commands/storage">Storage</a></li>
              <li><a href="/commands/cookies">Cookies</a></li>
              <li><a href="/commands/spies-stubs-clocks">Spies, Stubs &amp; Clocks</a></li>
            </ul>
          </li>
          <li><a href="/utilities">Utilities</a></li>
          <li><a href="/cypress-api">Cypress API</a></li>
        </ul>
        <ul class="nav navbar-nav pull-right">
          <li><a href="https://github.com/cypress-io/cypress-example-kitchensink">GitHub</a></li>
        </ul>
      </div>
    </div>
  </nav>

  <div class="banner">
    <div class="container">
      <h1>Storage</h1>
      <p>Examples of managing localStorage and sessionStorage in Cypress, for a full reference of commands, go to <a href="https://on.cypress.io/api" target="_blank">docs.cypress.io</a>
      </p>
    </div>
  </div>
  <div class="container content-container">
    <div id="clearlocalstorage">
      <div class="row">

        <div class="col-xs-7">
          <h4><a href="https://on.cypress.io/clearlocalstorage">cy.clearLocalStorage()</a></h4>
          <p>To clear all data in localStorage for the current origin, use the <a href="https://on.cypress.io/clearlocalstorage"><code>cy.clearLocalStorage()</code></a> command.</p>
          <pre><code class="javascript">cy.get('.ls-btn').click()
cy.get('.ls-btn').should(() => {
  expect(localStorage.getItem('prop1')).to.eq('red')
  expect(localStorage.getItem('prop2')).to.eq('blue')
  expect(localStorage.getItem('prop3')).to.eq('magenta')
})

cy.clearLocalStorage()
cy.getAllLocalStorage().should(() => {
  expect(localStorage.getItem('prop1')).to.be.null
  expect(localStorage.getItem('prop2')).to.be.null
  expect(localStorage.getItem('prop3')).to.be.null
})

cy.get('.ls-btn').click()
cy.get('.ls-btn').should(() => {
  expect(localStorage.getItem('prop1')).to.eq('red')
  expect(localStorage.getItem('prop2')).to.eq('blue')
  expect(localStorage.getItem('prop3')).to.eq('magenta')
})

// Clear key matching string in localStorage
cy.clearLocalStorage('prop1')
cy.getAllLocalStorage().should(() => {
  expect(localStorage.getItem('prop1')).to.be.null
  expect(localStorage.getItem('prop2')).to.eq('blue')
  expect(localStorage.getItem('prop3')).to.eq('magenta')
})

cy.get('.ls-btn').click()
cy.get('.ls-btn').should(() => {
  expect(localStorage.getItem('prop1')).to.eq('red')
  expect(localStorage.getItem('prop2')).to.eq('blue')
  expect(localStorage.getItem('prop3')).to.eq('magenta')
})

// Clear keys matching regex in localStorage
cy.clearLocalStorage(/prop1|2/)
cy.getAllLocalStorage().should(() => {
  expect(localStorage.getItem('prop1')).to.be.null
  expect(localStorage.getItem('prop2')).to.be.null
  expect(localStorage.getItem('prop3')).to.eq('magenta')
})</code></pre>
        </div>
        <div class="col-xs-5">
          <div class="well">
            <button class="ls-btn btn btn-success">Populate localStorage and sessionStorage</button>
          </div>
        </div>

        <div class="col-xs-12"><hr></div>
      </div>
    </div>

    <div id="getalllocalstorage">
      <div class="row">

        <div class="col-xs-7">
          <h4><a href="https://on.cypress.io/getalllocalstorage">cy.getAllLocalStorage()</a></h4>
          <p>To get all data in localStorage for all origins, use the <a href="https://on.cypress.io/getalllocalstorage"><code>cy.getAllLocalStorage()</code></a> command.</p>
          <pre><code class="javascript">cy.get('.ls-btn').click()

// getAllLocalStorage() yields a map of origins to localStorage values
cy.getAllLocalStorage().should((storageMap) => {
  expect(storageMap).to.deep.equal({
    // other origins will also be present if localStorage is set on them
    'http://localhost:8080': {
      prop1: 'red',
      prop2: 'blue',
      prop3: 'magenta',
    },
  })
})</code></pre>
        </div>
        <div class="col-xs-5">
        </div>

        <div class="col-xs-12"><hr></div>
      </div>
    </div>

    <div id="clearalllocalstorage">
      <div class="row">

        <div class="col-xs-7">
          <h4><a href="https://on.cypress.io/clearalllocalstorage">cy.clearAllLocalStorage()</a></h4>
          <p>To clear all data in localStorage for all origins, use the <a href="https://on.cypress.io/clearalllocalstorage"><code>cy.clearAllLocalStorage()</code></a> command.</p>
          <pre><code class="javascript">cy.get('.ls-btn').click()

// clearAllLocalStorage() yields null
cy.clearAllLocalStorage()
cy.getAllLocalStorage().should(() => {
  expect(localStorage.getItem('prop1')).to.be.null
  expect(localStorage.getItem('prop2')).to.be.null
  expect(localStorage.getItem('prop3')).to.be.null
})</code></pre>
        </div>
        <div class="col-xs-5">
        </div>

        <div class="col-xs-12"><hr></div>
      </div>
    </div>

    <div id="getallsessionstorage">
      <div class="row">

        <div class="col-xs-7">
          <h4><a href="https://on.cypress.io/getallsessionstorage">cy.getAllSessionStorage()</a></h4>
          <p>To get all data in sessionStorage for all origins, use the <a href="https://on.cypress.io/getallsessionstorage"><code>cy.getAllSessionStorage()</code></a> command.</p>
          <pre><code class="javascript">cy.get('.ls-btn').click()

// getAllSessionStorage() yields a map of origins to sessionStorage values
cy.getAllSessionStorage().should((storageMap) => {
  expect(storageMap).to.deep.equal({
    // other origins will also be present if sessionStorage is set on them
    'http://localhost:8080': {
      prop4: 'cyan',
      prop5: 'yellow',
      prop6: 'black',
    },
  })
})</code></pre>
        </div>
        <div class="col-xs-5">
        </div>

        <div class="col-xs-12"><hr></div>
      </div>
    </div>

    <div id="clearallsessionstorage">
      <div class="row">

        <div class="col-xs-7">
          <h4><a href="https://on.cypress.io/clearallsessionstorage">cy.clearAllSessionStorage()</a></h4>
          <p>To clear all data in sessionStorage for all origins, use the <a href="https://on.cypress.io/clearallsessionstorage"><code>cy.clearAllSessionStorage()</code></a> command.</p>
          <pre><code class="javascript">cy.get('.ls-btn').click()

// clearAllSessionStorage() yields null
cy.clearAllSessionStorage()
cy.getAllSessionStorage().should(() => {
  expect(sessionStorage.getItem('prop4')).to.be.null
  expect(sessionStorage.getItem('prop5')).to.be.null
  expect(sessionStorage.getItem('prop6')).to.be.null
})</code></pre>
        </div>
        <div class="col-xs-5">
        </div>

        <div class="col-xs-12"><hr></div>
      </div>
    </div>
  </div>

  <script src="/assets/js/vendor/jquery-1.12.0.min.js"></script>
  <script src="/assets/js/vendor/bootstrap.min.js"></script>
  <script src="/assets/js/vendor/highlight.pack.js"></script>
  <script src="/assets/js/scripts.js"></script>
</body>
</html>
